from favorites.models import Favorite
from django import template

register = template.Library()

class FavoritesForUserNode(template.Node):
    def __init__(self, user, context_var):
        self.user = user
        self.context_var = context_var

    def render(self, context):
        favorites = Favorite.objects.get_favorites_by_user(self.user)
        context[self.context_var] = favorites
        return ''


def do_favorites_for_user(parser, token):
    bits = token.contents.split()
    len_bits = len(bits)
    if len_bits != 4:
        raise TemplateSyntaxError(_('%s tag requires 3 arguments') % bits[0])
    if bits[2] != 'as':
        raise TemplateSyntaxError(_("second argument %s tag must be 'as'") % bits[0])
    return FavoritesForUserNode(bits[1],bits[3])
    
register.tag(do_favorites_for_user, "favorites_for_user")